package com.admin.system.modules.system.service.impl;

import com.admin.common.utils.PageResult;
import com.admin.common.utils.PageUtil;
import com.admin.core.dto.JobQueryDTO;
import com.admin.core.entity.SysJob;
import com.admin.core.mapper.JobMapper;
import com.admin.system.modules.system.service.JobService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author sq-lan
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, SysJob> implements JobService {

    @Override
    public PageResult<SysJob> paging(JobQueryDTO dto) {
        PageHelper.startPage(dto.getCurrent(), dto.getSize());
        List<SysJob> list = this.list(dto);
        return PageUtil.toPageHelper(list);
    }

    @Override
    public List<SysJob> list(JobQueryDTO dto) {
        return this.list(createWrapper(dto));
    }

    private LambdaQueryWrapper<SysJob> createWrapper(JobQueryDTO dto) {
        LambdaQueryWrapper<SysJob> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasText(dto.getName())) {
            wrapper.like(SysJob::getName, dto.getName());
        }
        if (dto.getEnabled() != null) {
            wrapper.eq(SysJob::getEnabled, dto.getEnabled());
        }
        if (dto.getCreateTime() != null && !dto.getCreateTime().isEmpty()) {
            wrapper.between(SysJob::getCreateTime, dto.getCreateTime().get(0), dto.getCreateTime().get(1));
        }
        return wrapper;
    }
}
